package com.example.demo.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;

import com.example.demo.controller.Result;
import com.example.demo.sys.mapper.TeacherMapper;
import com.example.demo.sys.entity.Teacher;

import org.springframework.web.bind.annotation.RestController;
import com.example.demo.common.tools.BaseController;

/**
 * <p>
    *  前端控制器 chrimer
    * </p>
 *
 * @author chrimer
 * @since 2020-06-26
 */
@RestController
@RequestMapping("/sys/teacher")
public class TeacherController extends BaseController {
    @Autowired
    private TeacherMapper teacherMapper;

    @RequestMapping(method= RequestMethod.POST,value="/page")
    public Result page(
        Integer pageNum,
        Integer pageSize) {
        Result result = new Result();
        Page<Teacher> page = new Page<Teacher>(pageNum, pageSize);
    Teacher parms = new Teacher();
        QueryWrapper<Teacher> warpper = new QueryWrapper<Teacher>(parms);

        result.setData(teacherMapper.selectPage(page, warpper));
        return result;
    }

    //查看个人信息
    @RequestMapping(method= RequestMethod.POST,value="/select")
    public Result select(String id) {

        Result result = new Result();
        Teacher studentselect = new Teacher();
        QueryWrapper<Teacher> warpper = new QueryWrapper<Teacher>(studentselect);
        warpper.eq("userid",id);
        result.setData(teacherMapper.selectOne(warpper));
        return result;
    }

    @RequestMapping(method= RequestMethod.POST,value="/save")
    public Result save(@RequestBody Teacher entity) {
        Result result = new Result();
        if (entity.getId() == null) {
    teacherMapper.insert(entity);
        } else {
    teacherMapper.updateById(entity);
        }
        return result;
    }
    @RequestMapping(method= RequestMethod.DELETE,value="/delete")
    public Result delete( String ids) {
        Result result = new Result();
        List<String> deleteIds = new ArrayList<String>();
        for (String id : ids.split(",")) {
            deleteIds.add(id);
        }
    teacherMapper.deleteBatchIds(deleteIds);
        return result;
    }

}
